package com.szitrus.smp.scalseal.service.impl;

import com.szitrus.smp.scalseal.entity.SealDO;
import com.szitrus.smp.scalseal.exception.BusinessException;
import com.szitrus.smp.scalseal.exception.CommonErrorCodeEnum;
import com.szitrus.smp.scalseal.service.ScaleSealService;
import com.szitrus.smp.scalseal.util.ImageUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述：
 *
 * @author ssl
 * @create 2020/09/21 10:30
 */
@Service
public class ScaleSealServiceImpl implements ScaleSealService {
    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    private String updateSql = "UPDATE SEAL SET WIDTH = ?, HEIGHT = ?, IMAGE = ? WHERE ID = ?";

    /**
     * 查询源印章集合
     *
     * @param querySql
     * @return
     */
    @Override
    public List<SealDO> listSeal(String querySql) throws BusinessException {
        List<SealDO> list = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(SealDO.class));
        if (null == list || list.isEmpty()) {
            throw new BusinessException(CommonErrorCodeEnum.B_1001);
        }
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSeal(SealDO sealDO) throws BusinessException {
        String newImg = ImageUtil.scale(sealDO.getImage(), sealDO.getWidth(), sealDO.getHeight());
        jdbcTemplate.update(updateSql, sealDO.getWidth(), sealDO.getHeight(), newImg, sealDO.getId());
    }
}
